<template>
  <!-- 关联的探测器 -->
  <div class="detector-list">
    <el-tag
      v-for="tag in multipleSelection"
      :key="tag.name"
      @close="handleClose(tag)"
      closable>
      {{tag.name}}
    </el-tag>
    <!-- 查询条件 -->
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" @submit.native.prevent>
        <el-form-item>
          <el-input
            placeholder="搜索"
            v-model="searchForm.name"
            @keyup.enter.native="search"
            clearable>
          </el-input>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 数据列表 -->
    <el-table class="naturaWater-table"
      v-loading="loading"
      ref="naturalWaterTable"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label=""  prop="name">
        <template slot-scope="scope">
          【{{scope.row.organizationName}}】{{scope.row.name}}
        </template>
      </el-table-column>
    </el-table>
    <div style="width:100%;text-align: right;" id="detectorBtnDiv">
         <el-button type="primary" class="verifyidentity-button" @click="saveDetectorTag" round>确定</el-button>
    </div>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
  import { getFcfSelectList } from '@/api/fcf'
  export default {
    name: 'DetectorList',
    props: {
      closeRelationFun: {
        type: Function,
        required: true
      },
      addDetectorFun: {
        type: Function,
        required: true
      },
      naturalWaterInfo:{
        type: Object
      }
    },
    data() {
      return {
        //选中的数据
        multipleSelection: [],
        pageLoading: false,
        loading: false,
        // 搜索条件
        searchForm: {
          name: ''
        },
        // 列表数据
        tableData: [],
        // 分页
        currentPage: 1,
        pageSize: 10,
        total: 0,
      }
    },
    created() {//入口
      let that = this
      // 加载列表
      that.getTableData()
    },
    methods: {
      // 获取列表数据
      getTableData(pageIndex, pageSize) {
        let that = this
        that.loading = true
        if (pageIndex) {
          that.currentPage = pageIndex
        }
        if (pageSize) {
          that.pageSize = pageSize
        }
        let parames = {
          organizationId: 0,
          fbsId: 0,
          parentId: 0,
          name: that.searchForm.name.trim(),
          pageSize: that.pageSize,
          pageIndex: that.currentPage - 1
        }
        // 获取列表信息
        getFcfSelectList(parames).then(res => {
          that.tableData = res.data
          that.total = res.total
          that.loading = false

          if(this.naturalWaterInfo.fcfsId!=null){
            var jsonobj = {"id":that.naturalWaterInfo.fcfsId,"name": that.naturalWaterInfo.fcfName}
            that.multipleSelection[0] = jsonobj
            for(var i=0;i<this.tableData.length;i++){
               if(this.tableData[i].id==this.naturalWaterInfo.fcfsId){
                   this.multipleSelection[0] = this.tableData[i]
                   that.$nextTick(() => {
                       this.$refs.naturalWaterTable.toggleRowSelection(this.tableData[i])
                   })
                   break
               }
            }
          }
        })
      },
      search() {
        this.getTableData(1)
      },
      handleSizeChange(pageSize) {
        this.getTableData(1, pageSize)
      },
      handleCurrentChange(currentPage) {
        this.getTableData(currentPage)
      },
      handleSelectionChange(val) {
         this.multipleSelection = val
         if (val.length > 1) {
            this.$refs.naturalWaterTable.clearSelection()
            this.$refs.naturalWaterTable.toggleRowSelection(val.pop())
        }

      },
      handleClose(tag) {
        this.multipleSelection.splice(this.multipleSelection.indexOf(tag), 1)
      },
      saveDetectorTag(){
        this.addDetectorFun(this.multipleSelection)
      }
    }

  }
</script>
<style lang="scss">
.detector-list {
  .el-tag {
    margin-right: 10px;
    margin-bottom: 10px;
  }

  .naturaWater-table .has-gutter{
          display: none;
  }

  .naturaWater-table .el-checkbox__inner {
      border-radius: 50px!important;
  }

  #detectorBtnDiv .el-button{
    display: unset!important;
    margin-top: 10px;
  }
  thead {
    .el-table-column--selection {
      .cell {
        display: none;
      }
    }
  }
}
</style>
